package com.gl.order.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gl.order.commom.ResultBean;
import com.gl.order.entity.TRole;
import com.gl.order.entity.dto.RoleDto;
import com.gl.order.entity.dto.RolePageDto;
import com.gl.order.entity.dto.UpdateRoleDto;
import com.gl.order.entity.vo.RoleVo;
import com.gl.order.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @Author: liangSY
 * @Date: 2021/6/22 14:52
 * @ClassName: RoleController
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("/role")
public class RoleController {


    private RoleService roleService;

    @Autowired
    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @GetMapping("/")
    @ApiOperation("获取角色列表")
    public ResultBean<List<TRole>> list() {
        List<TRole> roles = roleService.list();
        return ResultBean.success(roles);
    }

    @PostMapping("/page")
    @ApiOperation("获取角色列表")
    public ResultBean<IPage<RoleVo>> page(@RequestBody @Valid RolePageDto dto) {
        IPage<RoleVo> roles = roleService.pageOfRole(dto);
        return ResultBean.success(roles);
    }

    @GetMapping("/{id}")
    @ApiOperation("获取角色列表")
    @ApiImplicitParam(name = "id", value = "角色id", dataType = "String", paramType = "path")
    public ResultBean<TRole> list(@PathVariable("id") String id) {
        TRole role = roleService.findById(id);
        return ResultBean.success(role);
    }


    @PostMapping("/add")
    @ApiOperation("添加角色")
    public ResultBean addRole(@RequestBody @Valid RoleDto dto) {
        TRole role = roleService.findByNmaeOrCode(dto.getName(), dto.getRoleCode());
        if (!Objects.isNull(role)) {
            return ResultBean.failed("角色已存在");
        }
        roleService.add(dto);
        return ResultBean.success();
    }


    @PostMapping("/edit")
    @ApiOperation("修改角色")
    public ResultBean updateRole(@RequestBody @Valid UpdateRoleDto dto) {
        roleService.updateRole(dto);
        return ResultBean.success();
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除角色")
    @ApiImplicitParam(name = "id", value = "角色id", dataType = "String", paramType = "path")
    public ResultBean delRole(@PathVariable("id") String id) {
        roleService.delRole(id);
        return ResultBean.success();
    }

    @PostMapping("/delRoles")
    @ApiOperation("删除角色")
    @ApiImplicitParam(name = "id", value = "角色id", dataType = "String", paramType = "path")
    public ResultBean delRoles(@RequestBody List<String> ids) {
        roleService.delRoles(ids);
        return ResultBean.success();
    }


}
